<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php 
	$current_dbprefix = $this->db->dbprefix;
	$this->db->set_dbprefix('default_');
	 
	$DEFAULT_COUNTRY = getCountryInfoFromUserGeoLocation();
	$pre = array_keys(getCountriesOfEscortsUsed());
	$preCountry = ($DEFAULT_COUNTRY) ? $DEFAULT_COUNTRY->getId() : $pre[0];
	
	$page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0 ;
	$records_per_page = RECORD_PER_PAGE; 
	$escortCategoryId = getKeyOfArray( getSiteCategoryBaseOnSiteSetting() );
/**	
	$total = $this->db->select(TBL_ESCORT.".*")->from(TBL_ESCORT)->
			join(TBL_USER,TBL_ESCORT.'.user_id='.TBL_USER.'.id','left')->
			where( array( TBL_USER.'.permission_slug '=>'escort', TBL_USER.'.status'=> 'activated' ))->
			count_all_results();
**/
	
	$rs = $this->db->query("SELECT * FROM ".TBL_PLACE_LIST." WHERE country_id=$preCountry")->result();
	if($rs){
		$tmpArr = array();
		foreach($rs as $item){
			$regexp = "[[:<:]]{$item->id}[[:>:]]";
			$tmpArr[] = " places_list_ids_contract REGEXP '$regexp' ";
		}
		$listingPlaceCond = " (".implode(" OR ",$tmpArr).") ";
	}else{
		$listingPlaceCond = "1=0";
	}
	
	$query = "SELECT * FROM ".TBL_ESCORT." WHERE published=1 AND $listingPlaceCond AND escort_category_id=$escortCategoryId ORDER BY order_value DESC";
	
	//$allResult = $this->db->where( array('published'=>1,'country_id'=>$preCountry) )->order_by('point','desc')->get(TBL_ESCORT)->result();	
	$allResult = $this->db->query($query)->result();
	$total = count($allResult);
	 
	$pagination = create_pagination( 
					$uri = get_language_code().'/home/browse/?', 
					$total_rows = $total , 
					$limit= $records_per_page,
					$uri_segment = 0,
					TRUE, TRUE 
				);
/**							
	$records = $this->db->select(TBL_ESCORT.".*")->from(TBL_ESCORT)->
			join(TBL_USER,TBL_ESCORT.'.user_id='.TBL_USER.'.id','left')->
			where( array( TBL_USER.'.permission_slug '=>'escort', TBL_USER.'.status'=> 'activated' ))
			->limit($records_per_page, $page )->get()->result();

	$records = $this->db->where( array('published'=>1,'country_id'=>$preCountry) )->order_by('point','desc')
			->limit($records_per_page, $page )->get(TBL_ESCORT)->result();
**/
	$records = $this->db->query($query." LIMIT ".$page." ,$records_per_page ;")->result();			
	$measureOptionArray = getMeasureOptionData_ioc();
	$_SESSION['backURL'] = str_replace('browse','index',fullURL() );
	
	$_SESSION['pagination_result'] = array();
	$i = 1;
	foreach($allResult as $item){
		$_SESSION['pagination_result'][$item->id] = array( $i++ => $item->id);
	}
	$_SESSION['totalRESULTsearch'] = $total;
	
	$_SESSION['location_ids'] = $preCountry.'|0|0|0';
?>


<?php $this->db->set_dbprefix($current_dbprefix);?>	

<div class="home-escort-list">
		<?php $this->load->view("home/display_escort_info", array('records'=>$records,'item_per_row'=>4,'page'=>'BROWSE') ) ;?>
		
		<div class="clear"></div>
		<div class="pagging">
			<div id="browse_escort">
				<?php echo $pagination['links'];?>
			</div>
		</div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        scroll(0,0);
    });
</script>
    
    
	